package com.youpai.platform.file.service.service.impl;

import com.qingstor.sdk.exception.QSException;
import com.qingstor.sdk.service.Bucket;
import com.qingstor.sdk.service.QingStor;
import com.youpai.platform.common.constant.NumberConstant;
import com.youpai.platform.common.enums.ProcessCodeEnum;
import com.youpai.platform.common.exceptions.BaseBizException;
import com.youpai.platform.file.service.entity.resp.FileUploadResp;
import com.youpai.platform.file.service.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.*;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @date: 2021/11/28 23:06
 * @author: Anna
 * @version: 1.0
 * @description: 文件service实现
 */
@Slf4j
@Service
public class FileServiceImpl implements FileService {


    @Resource
    private QingStor qingStor;

    @Override
    public FileUploadResp uploadFile(MultipartFile file) {
        if (null == file) {
            log.error("上传文件为空");
            throw new BaseBizException(ProcessCodeEnum.UP_LOAD_FILE_CAN_NOT_BE_NULL);
        }
        String realFileName = file.getOriginalFilename();
        if(StringUtils.isBlank(realFileName)){
            log.error("上传文件文件名为空");
            throw new BaseBizException(ProcessCodeEnum.UP_LOAD_FILE_NAME_CAN_NOT_BE_NULL);
        }
        String[] realName = realFileName.split("\\.");
        String path = ""+ ThreadLocalRandom.current().nextInt() + System.currentTimeMillis();
        if(realName.length == NumberConstant.TWO_POSITIVE){
            path = path + "." + realName[NumberConstant.ONE_POSITIVE];
        }
        // 创建一个临时文件
        File temporaryFile = new File(path);
        try {
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(temporaryFile));
            out.write(file.getBytes());
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            log.error(ProcessCodeEnum.FILE_NOT_EXIST.getDesc());
            throw new BaseBizException(ProcessCodeEnum.FILE_NOT_EXIST);
        } catch (IOException e) {
            throw new BaseBizException(ProcessCodeEnum.PARAM_ERROR);
        }
        String bucketName = "youpai-daka-file/img";
        String zone = "pek3b";
        // 获取bucket
        Bucket bucket = qingStor.getBucket(bucketName, zone);
        // 上传文件
        Bucket.PutObjectInput input = new Bucket.PutObjectInput();
        input.setBodyInputFile(temporaryFile);
        input.setContentLength(temporaryFile.length());
        try {
            Bucket.PutObjectOutput putObjectOutput = bucket.putObject(path, input);
            System.out.println(putObjectOutput);
        } catch (QSException e) {
            e.printStackTrace();
        }
        temporaryFile.deleteOnExit();
        String url = "https://pek3b.qingstor.com/youpai-daka-file/img/"+path;
        return new FileUploadResp().setFileUrl(url);
    }
}
